<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="js/jquery-3.4.1.min.js"></script>
    <style>
        div {
            height: 100px;
            width: 100px;
            background: red;
            position: relative;
        }
    </style>
</head>

<body>
    <div></div>
    <script>
        $(function () {
            let x = 0;
            let y = 0;
            $(document).keydown(function (e) {
                console.log(e.keyCode)
                if (e.keyCode == 37) {
                    if (y > 0) {
                        $('div').css('left', y -= 100);
                    }
                }
                if (e.keyCode == 38) {
                    if (x > 0) {
                        $('div').css('top', x -= 100);
                    }
                }
                if (e.keyCode == 39) {
                    $('div').css('left', y += 100);
                }
                if (e.keyCode == 40) {
                    x += 100;
                    $('div').css('top', x);
                }
            })
        })
    </script>
</body>

</html>